package com.app.view_controller;

import java.util.Iterator;
import java.util.Stack;

import com.app.alarm.R;
import com.app.model.AlarmDataManager;
import com.app.model.AlarmDataManager.AlarmLoader;
import com.app.service.AlarmService;
import com.app.util.ApplicationMediator;
import com.app.util.MessageListenerAdapter;
import com.app.view_controller.custom_view.TextViewWithFont;

import android.R.color;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.Animator.AnimatorListener;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.BitmapFactory.Options;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewPropertyAnimator;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("unused")
public class AlarmRecevierActivity extends FragmentActivity {
	private static final boolean DEBUG = true;
	private static final String TAG = "ALARMRECEIVERACTIVITY";
	public static final int EMPTY_PAGE_LEFT = 0;
	public static final int DEFAULT_PAGE = 1;
	public static final int EMPTY_PAGE_RIGHT = 2;
	public static final String IS_RECEIVERACTIVITY_FOREGROUND = "is foreground";
	public static final String MISCELLANEOUS_SHARED_PREFERENCES = "miscellaneous shared preferences";
	private AlarmDataManager mAlarmData;
	private Ringtone tone;
	private ViewPager pager;
	private AlarmRecevierActivity mContext;
	private CloseActivityReceiver receiver;

	public void onAttachedToWindow() {
		// make the activity shows even when the screen is locked.
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
		window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
				+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_receiver);
		overridePendingTransition(0, 0);
		SharedPreferences preferences = getSharedPreferences(MISCELLANEOUS_SHARED_PREFERENCES, MODE_PRIVATE);
		preferences.edit().putBoolean(IS_RECEIVERACTIVITY_FOREGROUND, true).commit();
		pager = (ViewPager) findViewById(R.id.viewPager);
		pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		pager.setCurrentItem(DEFAULT_PAGE);
		mContext = this;
		final Intent intent = getIntent();
		pager.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				ObjectAnimator.ofFloat(pager, "TranslationY", -pager.getHeight(), 0).setDuration(ContentActivity.ANIMATION_DURATION_MEDIUM).start();
			}
		});

		receiver = new CloseActivityReceiver();

	}

	@Override
	protected void onResume() {
		IntentFilter filter = new IntentFilter(AlarmService.ACTION_CLOSE_ACTIVITY);
		registerReceiver(receiver, filter);
		super.onResume();
	}

	public ViewPager getViewPager() {
		return pager;
	}

	class CloseActivityReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(AlarmService.ACTION_CLOSE_ACTIVITY)) {
				AlarmRecevierActivity.this.finish();
			}
		}
	}

	private class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			// Default constructor
			super(fm);
		}

		@Override
		public android.support.v4.app.Fragment getItem(int position) {
			// Chose what page to visualize (The activity will finish itself
			// when the first or the third one are chosen)
			switch (position) {
			case EMPTY_PAGE_LEFT:
				return (android.support.v4.app.Fragment) EmptyFragment.newInstance("");
			case DEFAULT_PAGE:
				return (android.support.v4.app.Fragment) AlarmReceiverFragment.newInstance("");
			case EMPTY_PAGE_RIGHT:
				return (android.support.v4.app.Fragment) EmptyFragment.newInstance("");
			default:
				return (android.support.v4.app.Fragment) AlarmReceiverFragment.newInstance("");
			}
		}

		@Override
		public int getCount() {
			return 3;
		}
	}

	@Override
	protected void onPause() {
		SharedPreferences preferences = getSharedPreferences(MISCELLANEOUS_SHARED_PREFERENCES, MODE_PRIVATE);
		preferences.edit().putBoolean(IS_RECEIVERACTIVITY_FOREGROUND, false).commit();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (DEBUG)
			Log.d(TAG, "ondestroy called");
		unregisterReceiver(receiver);
		super.onDestroy();
	}
}
